package com.linqi.freebie.components


import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.linqi.freebie.utils.CommandLineOutput

/**
 * 显示操作日志区域
 */
@Composable
fun LogCat(
    modifier: Modifier = Modifier
) {
    // 滚动状态
    val scrollState = rememberScrollState()

    // 自动滚动到底部的效果
    LaunchedEffect(CommandLineOutput.lines.size) {
        scrollState.animateScrollTo(scrollState.maxValue)
    }

    // 使用 Surface 作为容器，提供背景色和圆角等样式
    Surface(
        modifier = modifier,
        color = Color.Transparent,
        shape = MaterialTheme.shapes.medium
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(scrollState)
                .padding(16.dp)
        ) {
            CommandLineOutput.lines.forEach { entry ->
                Text(
                    text = entry.toString(), // 使用 LogEntry 的 toString() 方法
                    style = MaterialTheme.typography.bodyMedium.copy(
                        color = entry.level.color.copy(alpha = 0.7f),
                        fontSize = 12.sp
                    ),
                    modifier = Modifier.padding(vertical = 4.dp)
                )
            }
        }
    }
}